package com.wjjun.basemvpvm.databinding.adapter;

import android.databinding.BindingAdapter;
import android.graphics.Paint;
import android.text.TextUtils;
import android.widget.TextView;

import com.wjjun.basemvpvm.utils.TimeUtils;

/**
 * textView
 * Created by 67535 on 2018/5/3.
 */
public class TextViewAdapter {

    @BindingAdapter(value = {"timeFormat", "seconds"}, requireAll = false)
    public static void setTime(TextView textView, String timeFormat, long seconds) {
        textView.setText(TimeUtils.formatDateBySecond(seconds, timeFormat));
    }


    @BindingAdapter(value = {"valueOf"})
    public static void valueOf(TextView textView, Object value) {
        textView.setText(String.valueOf(value));
    }

    @BindingAdapter(value = {"format", "arg1", "arg2", "arg3"}, requireAll = false)
    public static void setResString(TextView textView, String format, Object arg1, Object arg2, Object arg3) {
        if (!TextUtils.isEmpty(format)) {
            if (arg1 != null && arg2 != null && arg3 != null) {
                textView.setText(String.format(format, arg1, arg2, arg3));
            } else if (arg1 != null && arg2 != null) {
                textView.setText(String.format(format, arg1, arg2));
            } else if (arg1 != null) {
                textView.setText(String.format(format, arg1));
            } else {
                textView.setText("");
            }
        }
    }


    @BindingAdapter(value = "strike")
    public static void setStrike(TextView textView, boolean strike) {
        if (strike) {
            textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        } else {
            textView.getPaint().setFlags(0);
        }
    }

    @BindingAdapter(value = "underLine")
    public static void setUnderLine(TextView textView, boolean underLine) {
        if (underLine) {
            textView.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        } else {
            textView.getPaint().setFlags(0);
        }
    }

}
